﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AlgoImpl;
using ApplLogic.ApplClasses;

namespace ApplLogic.LogicalServices
{
    public class ApplStatHandler
    {
        private ApplWorkerStatInWeek m_WorkerStatInWeek = null;
        private ShiftRequests m_Request = null;
        public DateTime WeekStartTime { get; set; }
        public DateTime WeekEndTime { get; set; }


        public ApplStatHandler(DateTime start, DateTime end, ShiftRequests requests)

        {
            WeekStartTime = start;
            WeekEndTime = end;
            m_Request = requests;
        }

        public void SaveStatForWeek()
        {
            m_WorkerStatInWeek = new ApplWorkerStatInWeek();
            foreach (string workerId in FinalResultHolder.Instance.FinalResultOfSCModul.Keys)
            {
                saveSingleWorkerStat(workerId);
            }
        }

        private void saveSingleWorkerStat(string workerId)
        {
            getDataFromAlgoResult(workerId);
            ApplDbMediators.ApplDbWorkerStatMediator.Instance.AddWorkerStat(m_WorkerStatInWeek, true);
        }

        private void getDataFromAlgoResult(string workerId)
        {
            m_WorkerStatInWeek.WeekStartTime = WeekStartTime;
            m_WorkerStatInWeek.WeekSEndTime = WeekEndTime;
            m_WorkerStatInWeek.WorkerRef = ApplDbMediators.ApplDbWorkerMediator.Instance.GetWorker(workerId);
            m_WorkerStatInWeek.TotalNumberOfShiftsGot = FinalResultHolder.Instance.FinalResultOfSCModul[workerId].Count;
            m_WorkerStatInWeek.TotalWantedShifts = m_Request.GetNumberOfwantedShiftsOfWorker(workerId);
            m_WorkerStatInWeek.TotalForbiddenShifts = m_Request.GetNumberOfforbiddenShiftsOfWorker(workerId);
            calcShiftsThatWorkerAskedAndGot();
            calcShiftsThatWorkerAskedNotToBeAndGot();
            calcShiftsThatWorkerNotAskedAndGot();
        }

        private void calcShiftsThatWorkerNotAskedAndGot()
        {
            string workerId = m_WorkerStatInWeek.WorkerRef.PersonalDetails.Id;
            int counterOfShifts = 0;
            foreach (var shiftResult in FinalResultHolder.Instance.GetShiftResultOfWorker(workerId))
            {
                if (!m_Request.IsShiftSetForWorker(workerId, shiftResult.Id) && !m_Request.IsShiftForbidenForWorker(workerId, shiftResult.Id))
                {
                    counterOfShifts++;
                }
            }
            m_WorkerStatInWeek.ShiftsThatNotAskedAndGot = counterOfShifts;
        }

        private void calcShiftsThatWorkerAskedNotToBeAndGot()
        {
            string workerId = m_WorkerStatInWeek.WorkerRef.PersonalDetails.Id;
            int counterOfShifts = 0;
            foreach (var shiftId in m_Request.GetforbiddenShiftsOfWorker(workerId))
            {
                if (FinalResultHolder.Instance.IsShiftSetForWorker(workerId, shiftId))
                {
                    counterOfShifts++;
                }
            }
            m_WorkerStatInWeek.ShiftsThatAskedNotToBeAndGot = counterOfShifts;
        }

        private void calcShiftsThatWorkerAskedAndGot()
        {
            string workerId = m_WorkerStatInWeek.WorkerRef.PersonalDetails.Id;
            int counterOfShifts = 0;
            foreach (var shiftId in m_Request.GetWantedShiftsOfWorker(workerId))
            {
                if(FinalResultHolder.Instance.IsShiftSetForWorker(workerId, shiftId))
                {
                    counterOfShifts++;
                }
            }
            m_WorkerStatInWeek.ShiftsThatAskedAndGot = counterOfShifts;
        }

    }
}
